<?php

//运程定义控制器
class Haul_defineApp extends BackendApp
{
	var $_haul_define_mod;

	var $_haul_define_collect_mod;
	
	function __construct()
	{
		$this->Haul_defineApp();
	}

	function Haul_defineApp()
	{
		parent::BackendApp();

		$this->_haul_define_mod =& m('haul_define');
		
		$this->_haul_define_collect_mod =& m('haul_define_collect');
		
		
	}


	function index()
	{
		$h_name=empty($_GET['h_name'])?'':$_GET['h_name'];

		$h_linkman=empty($_GET['h_linkman'])?'':$_GET['h_linkman'];
		$conditions .= "1=1 AND h_linkman LIKE '%".$h_linkman."%' AND (h_name LIKE '%".$h_name."%' OR h_short_name LIKE '%".$h_name."%')";

		$page   =   $this->_get_page(10);   //获取分页信息

		$haul_define=$this->_haul_define_mod->find(array(
	        'fields'   => 'this.*', 
		    'conditions'  => $conditions,
	        'limit'   => $page['limit'],
			'order'   => 'h_tid desc',
			//'join'=>'belongs_to_dictionary',
        	'count'   => true   //允许统计
		));    //找出所有的运单
		$page['item_count']=$this->_haul_define_mod->getCount();   //获取统计数据


	    $this->_format_page($page);
	 	$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('haul_define', $haul_define);

		$this->assign('tabself', json_encode(array('text'=>'运程定义')));
		$this->display('haul_define.index.html');
	}

	
	 //添加
	function add()
	{
		if (!IS_POST)
		{
				$db=&db();
			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='haultype'";
			$typelist=array();
			$temp=$db->getAll($sql);
			
			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);
			
			
			$db=&db();
		$sql="SELECT d_value FROM zys_dictionary WHERE d_code='waybiltype'";
		$wstatus=$db->getAll($sql);
		$status=array();
		foreach ($wstatus as  $v){
			$status[$v['d_value']]=$v['d_value'];
		}
		$this->assign('status', $status);
			
			$this->assign('tabself', json_encode(array('text'=>'添加定义')));
            $this->assign('tabback', json_encode(array('text'=>'运程定义','url'=>'')));
		
			
            $this->import_resource('jquery.plugins/jquery.validate.js');			
			$this->display('haul_define.form.html');
		}
		else
		{
			$data = array(
			
				'h_short_name' => $_POST['h_short_name'],
                'h_name' => $_POST['h_name'],
                'h_address'  => $_POST['h_address'],
                'h_linkman'  => $_POST['h_linkman'],
			    'h_order'  => $_POST['h_order'],
				'h_linkman_tel'  => $_POST['h_linkman_tel'],
				'h_type'  => $_POST['h_type'],
				 'h_wm_type' => $_POST['h_wm_type'],
			);
    
			
	
			

			if (!$id = $this->_haul_define_mod->add($data))
			{
				$this->show_warning('添加失败',     '', 'index.php?app=haul_define&amp;act=add');
				return;
			}
			$this->show_message('添加成功',
                'back_list',    'index.php?app=haul_define',
                'continue_add', 'index.php?app=haul_define&amp;act=add'
                );
		}
	}
	
	
	
	
	 //添加运程定义采集信息
	function add_haul_define_collect()
	{
		
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		
		
		
		if (!IS_POST)
		{
				$db=&db();
			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
			$typelist=array();
			$temp=$db->getAll($sql);
			
			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);
			
		
			
			
			$this->assign('tabself', json_encode(array('text'=>'添加采集信息')));
            $this->assign('tabback', json_encode(array('text'=>'采集信息','url'=>'')));
		
			
            $this->import_resource('jquery.plugins/jquery.validate.js');			
			$this->display('haul_define_collect.form.html');
		}
		else
		{
			$data = array(
			
				'h_collect_type' => $_POST['h_collect_type'],
                'h_collect_des' => $_POST['h_collect_des'],
                'hc_h_tid'  => $id,
               
			);
    
			
	
			

			if (!$id = $this->_haul_define_collect_mod->add($data))
			{
				$this->show_warning('添加失败','', 'index.php?app=haul_define&amp;act=add');
				return;
			}
			$this->show_message('添加成功',
                'back_list',    'index.php?app=haul_define',
                'continue_add', 'index.php?app=haul_define&amp;act=add_haul_define_collect'
                );
		}
	}
	
	
	
	
	//编辑运单定义采集信息
	function edit_haul_define_collect()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);


   
		
		if (!IS_POST)
		{

			$haul_define=$this->_haul_define_collect_mod->get(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'hc_tid='.$id,
			));
	                $db=&db();
			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
			$typelist=array();
			$temp=$db->getAll($sql);
			
			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);
				

		  	
		
		
		  $haul_define_collect=$this->_haul_define_collect_mod->find(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'hc_h_tid='.$id,
			));
		
			
	    	$this->assign('haul_define_collect', $haul_define_collect);
		
		

			$this->assign('haul_define', $haul_define);
	
	        $this->assign('tabself', json_encode(array('text'=>'编辑采集信息')));
            $this->assign('tabback', json_encode(array('text'=>'采集信息','url'=>'')));
		

			$this->display('haul_define_collect.form.html');
		}else {
			
			    $data = array(
				'h_collect_type' => $_POST['h_collect_type'],
                'h_collect_des' => $_POST['h_collect_des'],
			 
            );
            
	

			
                /* 保存 */
            $rows = $this->_haul_define_collect_mod->edit($id, $data);
            if ($this->_haul_define_collect_mod->has_error())
            {
                $this->show_warning($this->_haul_define_mod->get_error()  , '',   'index.php?app=haul_define&amp;act=edit_haul_define_collect&amp;id=' . $id);
                return;
            }

            $this->show_message('编辑成功',
                'back_list',    'index.php?app=haul_define',
                '返回编辑',   'index.php?app=haul_define&amp;act=edit_haul_define_collect&amp;id=' . $id
            );
	
			
		}


	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	//编辑
	function edit()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);


   
		
		if (!IS_POST)
		{

			$haul_define=$this->_haul_define_mod->get(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'h_tid='.$id,
			));

			$db=&db();
			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='haultype'";
			$typelist=array();
			$temp=$db->getAll($sql);
			
			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);
			

		   $db=&db();
		$sql="SELECT d_value FROM zys_dictionary WHERE d_code='waybiltype'";
		$wstatus=$db->getAll($sql);
		$status=array();
		foreach ($wstatus as  $v){
			$status[$v['d_value']]=$v['d_value'];
		}
		$this->assign('status', $status);
			
		
		
		  $haul_define_collect=$this->_haul_define_collect_mod->find(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'hc_h_tid='.$id,
			));
		
			
	    	$this->assign('haul_define_collect', $haul_define_collect);
		
		

			$this->assign('haul_define', $haul_define);
	
	        $this->assign('tabself', json_encode(array('text'=>'编辑定义')));
            $this->assign('tabback', json_encode(array('text'=>'运程定义','url'=>'')));
		

			$this->display('haul_define.form.html');
		}else {
			
			    $data = array(
			    'h_short_name' => $_POST['h_short_name'],
                'h_name'  => $_POST['h_name'],
                'h_order'  => $_POST['h_order'],
                'h_address' => $_POST['h_address'],
			    'h_linkman' => $_POST['h_linkman'],
			    'h_linkman_tel' => $_POST['h_linkman_tel'],
			    'h_type' => $_POST['h_type'],
			    'h_wm_type' => $_POST['h_wm_type'],
			 
            );
            
	

			
                /* 保存 */
            $rows = $this->_haul_define_mod->edit($id, $data);
            if ($this->_haul_define_mod->has_error())
            {
                $this->show_warning($this->_haul_define_mod->get_error(),  '',   'index.php?app=haul_define&amp;act=edit&amp;id=' . $id);
                return;
            }

            $this->show_message('编辑成功',
                'back_list',    'index.php?app=haul_define',
                '返回编辑',   'index.php?app=haul_define&amp;act=edit&amp;id=' . $id
            );
	
			
		}


	}
	
	
	

	
	


	//删除采集信息
	function drop_haul_define_collect()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);

		if (!$this->_haul_define_collect_mod->drop($arrs))
		{
			$this->show_warning($this->_haul_define_collect_mod->get_error(),  '',   'index.php?app=haul_define&amp;act=edit&amp;id=' . $id);

			return;
		}
		$this->show_message('删除成功',  '',   'index.php?app=haul_define&amp;act=edit&amp;id=' . $id);
			
	}



	
	
	
	
	
	


	//删除
	function drop()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);

		if (!$this->_haul_define_mod->drop($arrs))
		{
			$this->show_warning($this->_haul_define_mod->get_error(),  '',   'index.php?app=haul_define');

			return;
		}
		$this->show_message('删除成功',  '',   'index.php?app=haul_define');
			
	}



	
	
	
}